package ms.shop.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.string.StringUtil;
import ms.shop.bean.AttrParamBean;
import ms.shop.bean.CommonQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.dao.AttrDAO;
import ms.shop.dao.ClassifyDAO;
import ms.shop.entity.Attr;
import ms.shop.entity.Banner;
import ms.shop.entity.Classify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: AttrService
 * Package Name: ms.shop.service
 * Date: 2018/6/4  10:30
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class AttrService {

    @Autowired
    private AttrDAO attrDAO;

    @Autowired
    private ClassifyDAO classifyDAO;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    public Page<Attr> query(CommonQueryParamBean commonQueryParamBean) {
        com.github.pagehelper.Page page = PageHelper.startPage(commonQueryParamBean.getPageNo(), commonQueryParamBean.getPageSize());
        List<Attr> attrs = attrDAO.queryAttrs();

        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), attrs);
    }

    /**
     * 查询属性list
     * @param attrParamBean
     * @return
     */
    public List<Attr> list(AttrParamBean attrParamBean) {
        return attrDAO.queryAttrList(attrParamBean.getClassifyId());
    }

    /**
     * 新增
     * @param attrParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(AttrParamBean attrParamBean) {
        Classify classify = classifyDAO.selectByPrimaryKey(attrParamBean.getClassifyId());
        if(StringUtil.isEmpty(classify)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "分类不存在");
        }
        //新增
        Attr attr = BeanUtil.copy(attrParamBean, Attr.class);
        attr.setStatus(2);
        attr.setCreateTime(System.currentTimeMillis());
        attr.setUpdateTime(System.currentTimeMillis());

        attrDAO.insert(attr);
    }

    /**
     * 修改
     * @param attrParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(AttrParamBean attrParamBean) {
        Classify classify = classifyDAO.selectByPrimaryKey(attrParamBean.getClassifyId());
        if(StringUtil.isEmpty(classify)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "分类不存在");
        }
        Attr currentAttr = attrDAO.selectByPrimaryKey(attrParamBean.getId());
        if(StringUtil.isEmpty(currentAttr)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "属性不存在");
        }
        //修改
        Attr attr = BeanUtil.copy(attrParamBean, Attr.class);
        attr.setStatus(null);
        attr.setCreateTime(null);
        attr.setUpdateTime(System.currentTimeMillis());

        attrDAO.updateByPrimaryKeySelective(attr);
    }

    /**
     * 启用
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        Attr attr = attrDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(attr)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "属性不存在");
        }
        if(1 == attr.getStatus()) {
            throw new BizException(ResponseCode.ATTR_STATUS_ERROR, "属性状态错误");
        }
        attr.setStatus(1);
        attrDAO.updateByPrimaryKeySelective(attr);
    }

    /**
     * 停用
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        Attr attr = attrDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(attr)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "属性不存在");
        }
        if(2 == attr.getStatus()) {
            throw new BizException(ResponseCode.ATTR_STATUS_ERROR, "属性状态错误");
        }
        attr.setStatus(2);
        attrDAO.updateByPrimaryKeySelective(attr);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public Attr detail(Long id) {
        Attr attr = attrDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(attr)) {
            throw new BizException(ResponseCode.ATTR_UN_EXIST, "属性不存在");
        }
        return attr;
    }

    /**
     * 清除缓存
     */
    public void clear() {
    }

}
